/**
 * 
 */
package org.wattdepot.cli.command;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import org.junit.Test;

/**
 * Tests the ChartPowerCommand command handler.
 * 
 * @author George Lee
 *
 */
public class TestChartCommand extends CommandTestBed {
  private static final String DATA_DIR = "data";
  private static final String OUTPUT_FILE = "test-kahe.html";

  /** 
   * Tests the doCommand method of the ChartPowerCommand command handler.
   * @see org.wattdepot.cli.command.CommandTestBed#testDoCommand()
   */
  @Override
  @Test
  public void testDoCommand() {
    String result, command;
    ChartCommand chart = new ChartCommand(this.client);
    
    // Sad tests
    command = "chart power foo SIM_KAHE "
      + "2009-11-01 2009-11-02 sampling-interval 120 file "
      + OUTPUT_FILE;
    result = chart.doCommand(command);
    assertTrue("Checking that the type parameter is invalid.", 
        result.startsWith(WattDepotCliCommand.PARAMETER_ERROR));
    
    command = "chart power generated foo"
      + " 2009-11-01 2009-11-02 sampling-interval 120 file "
      + OUTPUT_FILE;
    result = chart.doCommand(command);
    assertTrue("Checking that the source is invalid",
        result.startsWith(WattDepotCliCommand.SOURCE_ERROR));
    
    command = "chart power generated SIM_KAHE "
      + "foo 2009-11-02 sampling-interval 120 file "
      + OUTPUT_FILE;
    result = chart.doCommand(command);
    assertTrue("Checking that the timestamp result is invalid.",
        result.contains("Could not create timestamps."));
    
    command = "chart power generated SIM_KAHE "
      + "2009-11-01 2009-11-02 foo-interval 120 file "
      + OUTPUT_FILE;
    result = chart.doCommand(command);
    assertTrue("Checking that the interval parameter is invalid.", 
        result.startsWith(WattDepotCliCommand.PARAMETER_ERROR));
    
    command = "chart power generated SIM_KAHE "
      + "2009-11-01 2009-11-02 sampling-interval -100 file "
      + OUTPUT_FILE;
    result = chart.doCommand(command);
    assertTrue("Checking that the sampling interval is invalid.",
        result.startsWith(WattDepotCliCommand.INTERVAL_ERROR));
    
    command = "chart power generated SIM_KAHE"
      + " 2009-11-01 2009-11-02 sampling-interval 120 foo "
      + OUTPUT_FILE;
    result = chart.doCommand(command);
    assertTrue("Checking that the file parameter is invalid.", 
        result.startsWith(WattDepotCliCommand.PARAMETER_ERROR));
    
    // Test the chart command.
    result = chart.doCommand("chart power generated SIM_KAHE"
          + " 2009-11-01 2009-11-02 sampling-interval 120 file "
          + OUTPUT_FILE);
    assertTrue("Checking that we wrote the chart file.",
        result.startsWith("Wrote:"));
    assertTrue("Checking that the file name is at the end.",
        result.endsWith(DATA_DIR + File.separator + OUTPUT_FILE));
    File outputFile = new File(DATA_DIR + File.separator + OUTPUT_FILE);
    if (!outputFile.delete()) {
      fail("Could not remove file located at " + outputFile.getAbsolutePath());
    }
    
    result = chart.doCommand("chart power consumed SIM_KAHE"
        + " 2009-11-01 2009-11-02 sampling-interval 120 file "
        + OUTPUT_FILE);
    assertTrue("Checking that we wrote the chart file.",
        result.startsWith("Wrote:"));
    assertTrue("Checking that the file name is at the end.",
        result.endsWith(DATA_DIR + File.separator + OUTPUT_FILE));
    outputFile = new File(DATA_DIR + File.separator + OUTPUT_FILE);
    if (!outputFile.delete()) {
      fail("Could not remove file located at " + outputFile.getAbsolutePath());
    }
  }
}
